# 注意改get_data函数的几个参数
import json

import xlrd

import utils.util
from config import doc_dict_dir, HOTLINE_INCIDENT_DIFFICULTY, \
    HOTLINE_DIFFICULTY_LIBRARY, doc_data_dir


class ToLibrary:
    def get_data(self):
        # 先对处理完的INCIDENT文件进行排序
        utils.util.file_sorted(input_filename=INPUT_FILENAME)
        workbook = xlrd.open_workbook(INPUT_FILENAME, encoding_override="utf-8")
        table1 = workbook.sheets()[SHEET]
        classification_list = []
        incident_list = []
        difficulty_list = []
        for i in range(1, table1.nrows):
            # 内容分类
            Content_classification = table1.cell_value(i, CONTENT_CLASSIFICATION_COL)  # 内容分类
            classification_list.append(Content_classification)
            # incident列表
            incident = table1.cell_value(i, INCIDENT_COL)  # incident
            incident_list.append(incident)
            # 难度值列表
            difficulty = table1.cell_value(i, DIFFICULTY_COL)  # 难度
            difficulty_list.append(difficulty)

        row_num = table1.nrows
        list_4 = []
        type_repeat_list = []
        type_score = 0
        # 四层,内层为字典，外面套一层list，最后再套一层字典，最后最后再来一层列表
        # 最外面一个大列表，然后一个大字典，里面三个KV对，最后一个KV对的value是一个list，list里面是多个字典，每个字典里固定2个KV对
        for i in range(1, row_num):
            level = difficulty_list[i - 1]  # 难度list
            name = incident_list[i - 1]  # 事件list
            type = classification_list[i - 1]  # 内容分类list
            # 从第二行开始，如果当前行的事件内容跟之前的不一样，再创建
            if i > 1 and name == incident_list[i - 2]:
                continue
            else:
                dict_1 = dict()
                dict_1['name'] = name
                dict_1['level'] = level
                # print(dict_1)
                dict_3 = dict()

                # 记得改type_score
                if type not in type_repeat_list:  # 与之前的类别不同,开一个新list
                    list_2 = []
                    list_2.append(dict_1)  # 将内部的各个keyword字典装入list中
                    dict_3['type'] = type
                    dict_3['keyword'] = list_2
                    list_4.append(dict_3)
                    type_repeat_list.append(type)  # 去重作用
                # else重复了，沿用上次的list
                # elif type in type_repeat_list:
                else:
                    list_2.append(dict_1)
                    # type_score = dict_1['level'] + type_score
                    # 如果重复了，但是会直接加入到上一行的list中
        # 做type_score
        res = 0
        for en in list_4:
            res = 0
            for j in en['keyword']:
                res = j['level'] + res
            res = round(res / len(en['keyword']))
            # print(res)
            en['type_score'] = res

        # with open("../dict/difficulty_library.pkl", "w+", encoding="utf-8") as f:
        with open(OUTPUT_FILENAME, "w+", encoding="utf-8") as f:
            json.dump(list_4, f, indent=4, separators=(',', ':'), ensure_ascii=False)
            # print(json_data)


if __name__ == '__main__':
    # 几个参数
    # INPUT_FILENAME（输入的EXCEL文件名）
    INPUT_FILENAME = doc_data_dir + HOTLINE_INCIDENT_DIFFICULTY
    # OUTPUT_FILENAME(输出规则库的文件名)
    OUTPUT_FILENAME = doc_dict_dir + HOTLINE_DIFFICULTY_LIBRARY
    # 第几个sheet
    SHEET = 0
    # 内容分类的列数
    CONTENT_CLASSIFICATION_COL = 1
    # 实体事件的列数
    INCIDENT_COL = 2
    # 难度值的列数
    DIFFICULTY_COL = 3
    # ToLibrary.get_data()
    ToLibrary().get_data()
